/* ====================================================================
 * (#)Header: JmsMessenger.java
 * (#)Revision: 
 * (#)Date Created: 02-Nov-09 10:32:15 AM
 * (#)Created By: Karol Wozniak
 * ====================================================================
 *
 * Copyright (c) 2007 karolak .  All rights reserved.
 *
 * ====================================================================
 * 
 * The software application represented here is the sole property of 
 * karolak. Its unauthorized utilization is expressly prohibited.
 * 
 * ====================================================================
 */
package com.karolak.jms;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Session;
import javax.naming.InitialContext;

/**
 * <code>JmsMessenger</code> is used to ....
 * 
 * @author <a href="mailto:kwozniak@hotmail.com">Karol Wozniak</a>
 */
public abstract class JmsBase {
	private String destinationJndiName;
	private Connection connection;
	private Session session;
	private Destination destination;

	public JmsBase(String destinationJndiName) {
		try {
			InitialContext ctx = new InitialContext();
			ConnectionFactory connectionFactory = (ConnectionFactory) ctx.lookup("ConnectionFactory");
			setDestinationJndiName(destinationJndiName);
			setConnection(connectionFactory.createConnection());
			setSession(getConnection().createSession(false, Session.AUTO_ACKNOWLEDGE));
			setDestination(((Destination) ctx.lookup(destinationJndiName)));
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public JmsBase(Connection connection, String destinationJndiName) {
		try {
			InitialContext ctx = new InitialContext();
			setDestinationJndiName(destinationJndiName);
			setConnection(connection);
			setSession(getConnection().createSession(false, 1));
			setDestination(((Destination) ctx.lookup(destinationJndiName)));
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public void initialize2(String destinationJndiName) {
		try {
			InitialContext ctx = new InitialContext();
			ConnectionFactory connectionFactory = (ConnectionFactory) ctx.lookup("ConnectionFactory");
			setDestinationJndiName(destinationJndiName);
			setConnection(connectionFactory.createConnection());
			setSession(getConnection().createSession(false, 1));
			setDestination(((Destination) ctx.lookup(destinationJndiName)));
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public void start() {
		try {
			getConnection().start();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public void stop() {
		try {
			getConnection().stop();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public void close() {
		if (getSession() != null) {
			try {
				getSession().close();
			} catch (Exception e) {
			}
		}
		if (getConnection() != null) {
			try {
				getConnection().stop();
			} catch (Exception e) {
			}
		}
		if (getConnection() != null) {
			try {
				getConnection().close();
			} catch (Exception e) {
			}
		}
	}

	// === get/set === //
	public Connection getConnection() {
		return this.connection;
	}

	public void setConnection(Connection connection) {
		this.connection = connection;
	}

	public Destination getDestination() {
		return this.destination;
	}

	public void setDestination(Destination destination) {
		this.destination = destination;
	}

	public String getDestinationJndiName() {
		return this.destinationJndiName;
	}

	public void setDestinationJndiName(String destinationJndiName) {
		this.destinationJndiName = destinationJndiName;
	}

	public Session getSession() {
		return session;
	}

	public void setSession(Session session) {
		this.session = session;
	}
}
